package org.unei.common.helper;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;

/**
 * Created by sunlei on 2015/1/16.
 */
public class GsonHelper {
    private static Gson gson = new GsonBuilder().setPrettyPrinting()
            .setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    public static String toJson(Object obj) {
        return gson.toJson(obj);
    }

    public static String toJson(Object obj, Type type) {
        return gson.toJson(obj, type);
    }

    public static <T> T fromJson(String json, Class<T> cla) {
        return gson.fromJson(json, cla);
    }

    @SuppressWarnings({"unchecked", "hiding"})
    public static <Object> Object fromJson(String json, Type type) {
        return (Object) gson.fromJson(json, type);
    }

}
